package com.dunka.demo.controller;

import com.dunka.demo.pojo.Menu;
import com.dunka.demo.pojo.Menu_Grant;
import com.dunka.demo.pojo.Role;
import com.dunka.demo.service.MenuService;
import com.dunka.demo.service.Menu_GrantService;
import com.dunka.demo.service.RoleService;
import com.dunka.demo.util.ListComapre;
import com.dunka.demo.util.RestResponse;
import com.dunka.demo.util.RestResponseNew;
import com.dunka.demo.util.RestResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Dunka
 * @Description //TODO
 * @Date 12:29   2019/7/26
 * @ClassName Menu_GrantController
 */
@RestController
@RequestMapping("/menu_grant")
@Api(tags = "菜单权限管理相关接口",description = "提供菜单权限功能相关的RestAPI")
public class Menu_GrantController {
    @Autowired
    private Menu_GrantService menu_grantService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private RoleService roleService;

    /***
    * @Description: 传入用户id，获取该用户拥有的菜单id,以及对应的菜单名字
    * @Param: [role_id]
    * @return: java.lang.String
    * @Author: Dunka
    * @Date: 2019/7/26 14:35
    */
    @ApiImplicitParam(name = "GMC-ACCESS-TOKEN",value = "登录时得到的token",paramType = "header")
    @ApiOperation(value = "通过用户id获取对应角色所拥有的菜单项",
            notes = "传入参数：" +
                    "@RequestParam(value = \"user_id\") int user_id"+
                    "返回类型：" +
                    "Object 里存放的data 是 List<Menu> menuList",
            response = Object.class)
    @GetMapping("/getMenus")
    public Object getMenuNameByUserId(@RequestParam(value = "user_id") int user_id){
        RestResponseNew<List<Role>> response = roleService.listUserRoles(user_id);
        if (response==null){
            return RestResponseUtil.notFound();
        }
        List<Role> list =  response.getData();
        for (int i = 0; i <list.size() ; i++) {
            System.out.println(list.get(i).toString());
        }
        List<Integer> menuIdList = new ArrayList<Integer>();
        int role_id;
        for (int i = 0; i <list.size() ; i++) {
            role_id = list.get(i).getId();
            System.out.println(role_id);
            menuIdList.addAll(menu_grantService.getOwnMenu_idByRole_id(role_id));
        }

//        List<Integer> menuIdList =  menu_grantService.getOwnMenu_idByRole_id(role_id);
        List<Menu> menuList = new ArrayList<Menu>();
        for (Integer menuId : menuIdList) {
            Menu menu =  menuService.getMenuById(menuId);
            if(menu!=null){
                menuList.add(menu);
            }
        }
        if (!menuList.isEmpty()){
            return RestResponseUtil.success(menuList);
        }else{
            return RestResponseUtil.notFound();
        }
    }

    /***
     * @Description: 传入角色id，获取该角色拥有的菜单id,以及对应的菜单名字
     * @Param: [role_id]
     * @return: java.lang.String
     * @Author: Dunka
     * @Date: 2019/7/26 14:35
     */
    @ApiImplicitParam(name = "GMC-ACCESS-TOKEN",value = "登录时得到的token",paramType = "header")
    @ApiOperation(value = "通过用户id获取对应角色所拥有的菜单项",
            notes = "传入参数：" +
                    "@RequestParam(value = \"role_id\") int role_id"+
                    "返回类型：" +
                    "Object 里存放的data 是 List<Menu> menuList",
            response = Object.class)
    @GetMapping("/getMenusByRoleId")
    public Object getMenuNameByRoleId(@RequestParam(value = "role_id") int role_id){
        List<Integer> menuIdList =  menu_grantService.getOwnMenu_idByRole_id(role_id);
        List<Menu> menuList = new ArrayList<Menu>();
        for (Integer menuId : menuIdList) {
            Menu menu =  menuService.getMenuById(menuId);
            if(menu!=null){
                menuList.add(menu);
            }
        }
        if (!menuList.isEmpty()){
            return RestResponseUtil.success(menuList);
        }else{
            return RestResponseUtil.notFound();
        }
    }
    /***
    * @Description: 传入所有已勾选的菜单id数组,和对应的角色id
     * 过程，通过角色id获取之前的菜单id数组，新数组与旧数组对比，找出增加和减少的元素
     * 批量增加或者批量删除。
    * @Param: [menuIdList]
    * @return: boolean
    * @Author: Dunka
    * @Date: 2019/7/26 14:47
    */
    @ApiImplicitParam(name = "GMC-ACCESS-TOKEN",value = "登录时得到的token",paramType = "header")
    @ApiOperation(value = "通过角色id以及选中的菜单id更新该角色id所对应的菜单id",
            notes = "传入参数：" +
                    "@RequestBody List<Integer> menuIdList,@RequestParam(value = \"role_id\") int role_id"+
                    "返回类型：" +
                    "Object 里存放的data 是 boolean",
            response = Object.class)
    @PostMapping("/updateMenu_Grants")
    public Object updateMenu_Grant(@RequestBody List<Integer> menuIdList, @RequestParam(value = "role_id") int role_id){
        List<Integer> oldList = menu_grantService.getOwnMenu_idByRole_id(role_id);
        List<Integer> addList = ListComapre.getAddaListThanbList(menuIdList,oldList);
        List<Integer> deleteList = ListComapre.getDeleteaListThanbList(menuIdList,oldList);
        boolean addFlag = true,deleteFlag = true; //用于判断是否增加或者删除成功
         if(addList!=null && addList.size()>1){
            if(!menu_grantService.batchAddMenu_Grant(addList,role_id)){
                addFlag = false;
            }
        }else if(addList!=null && addList.size()==1){
            Menu_Grant menu_grant = new Menu_Grant();
            menu_grant.setMenu_id(addList.get(0));
            menu_grant.setRole_id(role_id);
             if(!menu_grantService.addMenu_Grant(menu_grant)){
                 addFlag = false;
             }
        }
        if (deleteList!=null&& deleteList.size()>1){
            if(!menu_grantService.batchDeleteMenu_Grant(deleteList,role_id)){
                deleteFlag = false;
            }
        }else if(deleteList!=null && deleteList.size()==1) {
            if(!menu_grantService.deleteMenu_Grant(deleteList.get(0), role_id)){
                deleteFlag = false;
            }
        }
        if (addFlag && deleteFlag){
            return RestResponseUtil.success();
        }else{
            return RestResponseUtil.failure("修改菜单权限失败");
        }
    }
}
